<?php


namespace App\Admin\Exports;


use App\Admin\Services\GoodService;
use App\Model\Good;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;

class GoodExport extends DefaultValueBinder implements FromQuery, WithMapping, ShouldQueue
{
    use Exportable;

    public $asinArr = [];


    public function __construct(array $asinArr = [])
    {
        $this->asinArr = $asinArr;
    }

    /**
     * @inheritDoc
     */
    public function query()
    {

        return Good::query()->limit(15);
//        return GoodService::getGoodByAsin($this->asinArr);
    }

    /**
     * @inheritDoc
     */
    public function map($row): array
    {


        return $row->toArray();
    }

    /**
     * @inheritDoc
     */
    public function drawings()
    {
        // TODO: Implement drawings() method.
        $amazonImgUrl = "http://img.susan.jiandanshang.com/upload/w_66/http://www.corciova.us/image1/A8_110_.jpg";
        $img = imagecreatefromjpeg($amazonImgUrl);

        $objDrawing = new MemoryDrawing();
        $objDrawing->setResizeProportional(false)
            ->setWidth(80)
            ->setHeight(80)
            ->setOffsetX(8)
            ->setOffsetY(8)
            ->setCoordinates("P2")
            ->setImageResource($img);
        $objDrawing->setRenderingFunction(MemoryDrawing::RENDERING_DEFAULT)
            ->setMimeType(MemoryDrawing::MIMETYPE_JPEG);

        return $objDrawing;
    }
}
